// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   StringUtil.java

package com.boci.common.util;

import java.text.*;
import java.util.Date;

public class StringUtil
{

    public StringUtil()
    {
    }

    public static String getReferenceNo(String pOrderID)
    {
        if(pOrderID == null || pOrderID.length() == 0)
        {
            return "";
        } else
        {
            String strTmp = pOrderID.substring(8, pOrderID.length());
            int intRefNo = Integer.parseInt(strTmp);
            return (new StringBuilder()).append("TRD ").append(intRefNo).toString();
        }
    }

    public static String formatException(Exception ex)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(ex.toString());
        sb.append("\n");
        StackTraceElement ste[] = ex.getStackTrace();
        for(int i = 0; i < ste.length; i++)
        {
            sb.append(ste[i].toString());
            sb.append("\n");
        }

        return sb.toString();
    }

    public static String formatPriceValue(String pValue, String pCcy)
    {
        if(pValue != null)
        {
            if(pValue.trim().equals(""))
                return null;
            else
                return (new StringBuilder()).append(pValue).append(" ").append(pCcy).toString();
        } else
        {
            return null;
        }
    }

    public static String formatPriceValue(String pValue)
    {
        return pValue;
    }

    public static String formatQtyValue(String pValue)
    {
        double doubleTmp;
        try
        {
            doubleTmp = Double.parseDouble(pValue);
        }
        catch(Exception e)
        {
            return pValue;
        }
        return qtyFormatter.format(doubleTmp);
    }

    public static String formatTableDateTime(String pValue)
    {
        Date date;
        try
        {
            date = inFormatter.parse(pValue);
        }
        catch(ParseException e)
        {
            return pValue;
        }
        return outTableFormatter.format(date);
    }

    public static String formatLastUpdatedDateTime(String pValue)
    {
        Date date;
        try
        {
            date = inFormatter.parse(pValue);
        }
        catch(ParseException e)
        {
            return pValue;
        }
        return outLastUpdateFormatter.format(date);
    }

    public static String formatLastUpdatedDateTime(Date pValue)
    {
        return outLastUpdateFormatter.format(pValue);
    }

    public static String formatOrderCancelLastUpdatedDateTime(Date pValue)
    {
        return outOrderCancelFormatter.format(pValue);
    }

    public static String replaceText(String pInString, String pPattern, String pNewText)
    {
        if(pInString == null || pInString.trim().equals("") || pPattern == null || pPattern.trim().equals(""))
            return pInString;
        if(pNewText == null)
            return pInString;
        int index = pInString.indexOf(pPattern);
        int len = pPattern.length();
        if(index < 0)
        {
            return pInString;
        } else
        {
            StringBuffer tmpStrBuf = new StringBuffer(pInString);
            tmpStrBuf.replace(index, index + len, pNewText);
            String strResult = tmpStrBuf.toString();
            return strResult;
        }
    }

    private static NumberFormat priceFormatter = new DecimalFormat("#,###,###.00");
    private static NumberFormat qtyFormatter = new DecimalFormat("#,###,###");
    private static SimpleDateFormat inFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
    private static SimpleDateFormat outLastUpdateFormatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    private static SimpleDateFormat outTableFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat outOrderCancelFormatter = new SimpleDateFormat("dd/MM/yyyy");

}
